// SPDX-License-Identifier: GPL-2.0-only

#include <linux/kallsyms.h>
#include <linux/delay.h>
#include <linux/file.h>
#include "accelerate_ecryptfs.h"

static func_ipsec_accelerate g_ipsec_hook;

void ecryptfs_accelerate_init(void)
{
	g_ipsec_hook = (func_ipsec_accelerate)__symbol_get("ipsec_speedup_ecryptfs");
}

void ecryptfs_accelerate_exit(void)
{
	g_ipsec_hook = NULL;
}

void ecryptfs_write_deley(void)
{
	static int count;

	if (++count > 50) {
		count = 0;
		msleep(50);
	}
}

int ecryptfs_accelerate(struct scatterlist *src,
	struct scatterlist *dst, struct ecryptfs_crypt_stat *crypt_stat,
	char *extent_iv, size_t extent_size, int op)
{
	int rc = -EINVAL;
	struct scatterlist *temp_src = NULL;
	struct scatterlist *temp_dst = NULL;

	if (g_ipsec_hook == NULL)
		return rc;

	temp_src = (op == 1) ? src : dst;
	temp_dst = (op == 1) ? dst : NULL;
	return (int)g_ipsec_hook(temp_src, temp_dst, crypt_stat, extent_iv, extent_size);
}

